package Dungeoneering.Puzzles;

import org.powerbot.game.api.methods.Widgets;
import org.powerbot.game.api.methods.input.Keyboard;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.wrappers.interactive.NPC;

import Dungeoneering.Entities.Room;
import Dungeoneering.Utils.Utilities;

public class Hoardstalker extends PuzzleStrategy {
	
	private enum BarrelItems { BANANA, FIREBREATH, MUSHROOM, POISON, EEL, VIAL };
	private enum ChestItems { BLOOD, COINS, ROD, NEEDLE, UNHOLY };
	private enum FloorItems { KITESHIELD, BONES, ASHES };
	private enum TableItems { HEADLESS, HAMMER, FEATHER, BOWSTRING };
	
	private static final int HOARDSTALKER_ID = 11011;
	private static final int OPENDOOR_ID = 49604;
	private static final int CLOSEDDOOR_ID = 49601;
	private static final int TABLE_ID = 50860;
	private static final int CHEST_ID = 49595;
	private static final int BARREL_ID = 49598;
	//TODO These 2 item ids
	private static final int INV_COINS_ID = 1337;
	private static final int INV_FEATHER_ID = 1337;
	
	/*private static final int UNHOLY_ID = 17383;
	private static final int KITESHIELD_ID =  17405;
	private static final int BONES_ID = 17387;
	private static final int ASHES_ID = 17389;
	private static final int BANANA_ID = 17381;
	private static final int FIREBREATH_ID = 17395;
	private static final int MUSHROOM_ID = 17407;
	private static final int POISON_ID = 17411;
	private static final int EEL_ID = 17397;
	private static final int VIAL_ID = 17413;
	private static final int BLOOD_ID = 17385;
	private static final int COINS_ID = 17391;
	private static final int ROD_ID = 17399;
	private static final int NEEDLE_ID = 17409;
	private static final int HAMMER_ID = 17401;
	private static final int FEATHER_ID = 17393; // use inventory item for vial, coin, hammer, feather
	private static final int HEADLESS_ID = 17403;
	private static final int BOWSTRING_ID = 17389;*/
	
	
	@Override
	public boolean detect(Room room) {
		for(NPC n : room.getMonsters()) {
			if(n.getId() == HOARDSTALKER_ID)
				return true;
		}	
		return false;
	}

	@Override
	public void solve(Room room) {
		// TODO solve it :p
		if(!detect(room))
			//TODO write an error message
			return;
		
		NPC Hoardstalker = null;
		for(NPC n : room.getMonsters())
			if(n.getId() == HOARDSTALKER_ID)
				Hoardstalker = n;
						
		String text = null;		
		
		while (text == null) {
			long l = System.currentTimeMillis();	
			Hoardstalker.interact("Get-Riddle"); // TODO replace this with your method	
			// TODO Rotate screen
			int rand = Random.nextInt(400, 600);
			while ((text = getText()) == null && System.currentTimeMillis() < (l + rand))
				Utilities.sleep(10);
		}
		
		doRiddle(text.substring(4, 13));
	}
	
	private String getText() {
		try {			
			return Widgets.get(1184).getChildren()[13].getText();	
		} catch(NullPointerException e) {
			return null;
		}		
	}
	
	private void doRiddle(String riddle) {
		if (riddle.equalsIgnoreCase("Without me, "))
			getTable(TableItems.BOWSTRING);
		else if (riddle.equalsIgnoreCase("Though I'm l"))
			getTable(TableItems.FEATHER);
		else if (riddle.equalsIgnoreCase("The blunt fo"))
			getTable(TableItems.HAMMER);
		else if (riddle.equalsIgnoreCase("Completed I c"))
			getTable(TableItems.HEADLESS);
		else if (riddle.equalsIgnoreCase("From your ve"))
			getChest(ChestItems.BLOOD);
		else if (riddle.equalsIgnoreCase("I pluck the "))
			getChest(ChestItems.ROD);
		else if (riddle.equalsIgnoreCase("I am cursed "))
			getChest(ChestItems.NEEDLE);
		else if (riddle.equalsIgnoreCase("I can get yo"))
			getChest(ChestItems.COINS);
		else if (riddle.equalsIgnoreCase("I demonstrat"))
			getChest(ChestItems.UNHOLY);
		else if (riddle.equalsIgnoreCase("Born through"))
			getFloor(FloorItems.ASHES);
		else if (riddle.equalsIgnoreCase("Sticks and s"))
			getFloor(FloorItems.BONES);
		else if (riddle.equalsIgnoreCase("Though my fr"))
			getFloor(FloorItems.KITESHIELD);
		else if (riddle.equalsIgnoreCase("Remove my ye"))
			getBarrel(false, BarrelItems.BANANA);
		else if (riddle.equalsIgnoreCase("I am water t"))
			getBarrel(false, BarrelItems.FIREBREATH);
		else if (riddle.equalsIgnoreCase("While many c"))
			getBarrel(false, BarrelItems.MUSHROOM);
		else if (riddle.equalsIgnoreCase("Without me yo"))
			getBarrel(true, BarrelItems.VIAL);
		else if (riddle.equalsIgnoreCase("The slowest "))
			getBarrel(true, BarrelItems.POISON);
		else if (riddle.equalsIgnoreCase("A serpent am"))
			getBarrel(true, BarrelItems.EEL);
		
		return;
	}
	
	private boolean getBarrel(boolean nextScreen, BarrelItems item) {
		
		Utilities.thinkSleep();
		//TODO Walk
		//Rotate Screen
		//"search" barrel
		if(nextScreen) {
			Utilities.thinkSleep();
			Keyboard.sendKey('4');
			Utilities.thinkSleep();
			Keyboard.sendKey((char)(item.ordinal() - 3));
		}
		else	
			Keyboard.sendKey((char)item.ordinal());
		//Start walking back
		return true;
	}
	
	private boolean getChest(ChestItems item) {
		
		Utilities.thinkSleep();
		if(invCheck(INV_COINS_ID)) {
			// TODO use coins on hoardstalker 
		}
		//TODO Walk
		//Rotate Screen
		//"search" chest
		Keyboard.sendKey((char)item.ordinal());
		//Start walking back
		return true;

	}
	
	private boolean getTable(TableItems item) {
		Utilities.thinkSleep();
		if(invCheck(INV_FEATHER_ID)) {
			// TODO use coins on hoardstalker 
		}
		//TODO Walk
		//Rotate Screen
		//pick up item
		//Start walking back
		 return true;
	}
	
	private boolean getFloor(FloorItems item) {
		//TODO Walk
		//Rotate Screen
		//pick up item
		//Start walking back
		return true;
	}
	
	private boolean invCheck(final int id) {
	    if(Inventory.getCount(id) > 1)
	    	return true;
	    return false;
	}
}


